﻿using JIG_SoftTemplate.ViewModels;
using JIG_SoftTemplate.Views;
using Microsoft.Extensions.DependencyInjection;
using System.Diagnostics;
using System.Reflection;
using System.Windows;

namespace JIG_SoftTemplate
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public static new App Current => (App)Application.Current;

        public IServiceProvider Services { get; }

        public App()
        {
            //配置Nlog
            NLog.LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration("NLog.config");

            #region 依赖注入配置
            var container = new ServiceCollection();

            
            //注册Snackbar（底部提示条）服务
            container.AddSingleton<Wpf.Ui.ISnackbarService, Wpf.Ui.SnackbarService>();
            //注册ContentDialog（提示框）服务
            container.AddSingleton<Wpf.Ui.IContentDialogService, Wpf.Ui.ContentDialogService>();
            //注册导航服务
            container.AddSingleton<Common.Services.NavigationService>();
            //注册日志服务
            container.AddSingleton<Common.Services.LoggerService>();
            //注册Json配置文件服务
            container.AddSingleton<Common.Services.JsonConfigService>();
            //注册语言切换服务
            container.AddSingleton<Common.Services.LangSwitchService>();

            //注册主窗口与ViewModel
            container.AddSingleton<MainWindow>();
            container.AddSingleton<MainWindowViewModel>();

            //注册Page页面
            container.AddSingleton<HomePage>();
            container.AddTransient<LoginPage>();
            container.AddTransient<SettingPage>();

            //注册Page的ViewModel
            container.AddSingleton<HomePageViewModel>();
            container.AddTransient<LoginPageViewModel>();
            container.AddTransient<SettingPageViewModel>();

            Services = container.BuildServiceProvider();
            #endregion


            #region 确保程序单实例运行
            //判断程序是否已经启动
            var ProcessName = Assembly.GetExecutingAssembly().GetName().Name;//程序集名称
            Process currentProcess = Process.GetCurrentProcess();//当运行的新进程

            // 通过程序集名称判断是否有进程已运行
            Process[] ps = Process.GetProcessesByName(ProcessName);

            // 因为新进程名也是ProcessName，所以ps的Length大于1时才是有多个进程
            if (ps.Length > 1)
            {
                foreach (var item in ps)
                {
                    // 找到非当前新进程的 MessageConstruction 进程对象
                    if (item.Id != currentProcess.Id)
                    {
                        //关闭之前的进程
                        item.Kill();
                    }
                }
            }
            #endregion

            //解决Textbox小数输入不了的问题
            FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false;
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            MainWindow = Services.GetRequiredService<MainWindow>();
            MainWindow.Show();
        }
    }

}
